﻿package com.victim.snake.room{
	
	import com.victim.snake.map.Map;
	import com.victim.snake.snake.Snake;	
	import com.victim.snake.player.Player;
	import flash.events.Event;
	import flash.utils.Dictionary;
	import flash.events.EventDispatcher;
	import com.victim.snake.player.PlayerEvent;
	import com.victim.snake.room.RoomEvent;
	import com.victim.snake.user.User;
	
	
	//房间
	public class Room extends EventDispatcher{
		private var master:Player=null;
		private var snakes:Dictionary=new Dictionary();	//所有蛇
		private var computers:Dictionary=new Dictionary();	//电脑玩家的蛇
		private var users:Dictionary=new Dictionary();	//其它玩家的蛇<snake>(除当前玩家外的其它玩家，不包括电脑)
		private var players:Array=new Array();	//房间内所有玩家
		private var roomTitle:String="";	//房间名
		private var id:int=0;	//id	
		private var roomType:int=0;	//房间状态	（0 等待  1 进行中
		private var computerCount=0;
		private var map:Map=null;
		private var gameMode:int=0;	//游戏模式：0吃货模式，1死亡模式
		private var modeCounter:int=0;	//游戏模式计数器
		private var groups:Groups=new Groups;	//队伍操作类
		
		//构造函数，分析房间字符串，设置房间信息
		public function Room(roomStr:String){
			
			var roomArray:Array = roomStr.split(";");	//分析房间消息
			this.id=roomArray[0];
			this.roomTitle=roomArray[1];
			
			for(var i:int=8;i<roomArray.length;i+=6){	//添加玩家
				if(roomArray[i+2]!=""){	//如果用户ID不为空
					var player:Player=null;
					
					if(User.getPlayer().getUid()==int(roomArray[i+2])){	//如果是当前玩家
						player=User.getPlayer();
					}else{	//如果不是当前玩家
						player=new Player();
						player.setUserName(roomArray[i+1]);
						player.setUid(roomArray[i+2]);

					}
					
					//设置玩家状态（是否准备）
					if(int(roomArray[i+3])==0){
						player.setIsReady(false);
					}else{
						player.setIsReady(true);
					}
					
					
					//设置玩家蛇样式
					player.setSnakeStyle(roomArray[i+4]);
					
					this.addPlayer(player,roomArray[i]);			
				
					if(player.getUid()==roomArray[4]){
						this.setMaster(player);
					}
					
					//设置玩家队伍
					this.groups.getGroupById(roomArray[i+5]).addPlayer(player);
				}
			}
			
			//设置地图
			this.setMap(roomArray[5]);
			//设置游戏模式
			this.gameMode=int(roomArray[6]);
			//设置游戏模式计数器值
			this.modeCounter=int(roomArray[7]);
			//trace(roomArray[5]);
		}
		
		
		//玩家准备状态改变事件,抛出房间内玩家准备事件
		private function playerReaderEvent(e:PlayerEvent):void{
			this.dispatchEvent(new RoomEvent(RoomEvent.PlayerReady,this,e.player,this.getPlayerSite(e.player)));
		}
		//玩家准备状态改变事件,抛出房间内玩家取消准备状态事件
		private function playerUnReadyEvent(e:PlayerEvent):void{			
			this.dispatchEvent(new RoomEvent(RoomEvent.PlayerUnReady,this,e.player,this.getPlayerSite(e.player)));
		}
		
		
		
		//初使化房间
		public function init():void{
			this.roomType=0;
			this.snakes=new Dictionary();
			this.computers=new Dictionary();
			this.users=new Dictionary();
			this.modeCounter=0;
		}
		
		
		//添加用户到房间(指定位置及队伍)，并反回用户在房间中的位置.如果返回-1,则添加失败  该方法在添加的同时会在player上添加PlayerEvent.Ready及PlayerEvent.UnReady监听
		public function addPlayer(player:Player,groupId:int,site:int=-1):int{
			if(site==-1){	//如果位置参数为-1,则在房间的第一个空位上添加玩家,如果没有空位,则返回添加失败(-1)
				for(var i:int=0;i<8;i++){
					if(this.players[i]==null){
						this.players[i]=player;
						site=i;
						break;
					}
				}
			}else{
				this.players[site]=player;
			}
			
			//将玩家添加到队伍中
			this.groups.getGroupById(groupId).addPlayer(player);
			
			//监听player是否改变准备状态
			player.addEventListener(PlayerEvent.Ready,playerReaderEvent);
			player.addEventListener(PlayerEvent.UnReady,playerUnReadyEvent);
			this.dispatchEvent(new RoomEvent(RoomEvent.AddPlayer,this,player,site));	//抛出新用户加入事件
			return site;
		}
		
		//设置房间中指定玩家到指定队伍
		public function setPlayerGroup(playerId:int,groupId:int):void{
			this.groups.getGroupById(groupId).addPlayer(this.getPlayerById(playerId));
		}
		
		
		public function getPlayers():Array{
			return players;
		}
		
		
		//返回玩家在房间中的位置
		public function getPlayerSite(player:Player):int{
			for(var i:int=0;i<8;i++){				
				if(this.players[i]==player){
					return i;
				}
			}
			return -1;
		}
		
		//按用户ID返回房间中的用户
		public function getPlayerById(uid:int):Player{
			var player:Player=null;
			for(var i:int=0;i<8;i++){				
				if(this.players[i]!=null){
					player=Player(players[i]);
					if(player.getUid()==uid){						
						return player;
					}
				}
			}
			return null;
		}
		
		//按用户ID移除用户,并反回被移除用户在房间中的位置
		public function reMovePlayer(player:Player):int{
			for(var i:int=0;i<8;i++){
				if(this.players[i]!=null){
					if(Player(this.players[i]).getUid()==player.getUid()){
						this.players[i]=null;
						this.dispatchEvent(new RoomEvent(RoomEvent.ExitPlayer,this,player,i));	//抛出有用户退出事件
						return i;
					}
				}
			}
			return -1;
		}
		
		
		//按用户ID设置房主,并反回被新房主在房间中的位置
		public function setMaster(player:Player):int{
			for(var i:int=0;i<8;i++){
				if(this.players[i]!=null){
					//trace(Player(this.players[i]).getUid()+";"+player.getUid());
					if(Player(this.players[i]).getUid()==player.getUid()){
						//trace(this.players[i].getUserName()+"为房主");
						this.master=this.players[i];
						this.dispatchEvent(new RoomEvent(RoomEvent.SetMaster,this,this.players[i],i));	//抛出设置房主事件
						return i;
					}
				}
			}
			return -1;
		}
		
		//测试用，显示房间字符串消息
		//public function toString():void{
//			var strRoom:String="";
//			strRoom=id+";"+roomTitle+";"+master.getUid()+";"+master.getUserName();
//			for(var i:int=0;i<players.length;i++){
//				strRoom=strRoom+";"+Player(players[0]).getUid()+";"+Player(players[0]).getUserName();
//			}
//			trace(strRoom);
//		}
		
		//从房间里删除某只蛇
		public function deleteSnake(snakeId:int):void{
			delete this.snakes[snakeId];
			delete this.users[snakeId];
			delete this.computers[snakeId];
		}
		
		///////get and set/////////////// 
		public function getMaster():Player{
			return this.master;
		}
		
		
		public function getSnakes():Dictionary{
			return this.snakes;
		}
		
		public function getSnake(i:int):Snake{
			return this.snakes[i];
		}		
		
		
		public function setSnake(snake:Snake):void{
			this.snakes[snake.getId()]=snake;
		}
		
		public function getComputers():Dictionary{
			return this.computers;
		}
		
		public function setComputer(snake:Snake):void{
			this.computers[snake.getId()]=snake;
		}
		
		public function getUsers():Dictionary{
			return this.users;
		}
		
		public function setUsers(snake:Snake):void{
			this.users[snake.getId()]=snake;
		}
		
		public function getId():int{
			return this.id;
		}
		
		public function setId(id:int):void{
			this.id=id;
		}
		
		public function getGameMode():int{
			return this.gameMode;
		}
		
		public function setGameMode(gameMode:int):void{
			this.gameMode=gameMode;
			this.dispatchEvent(new RoomEvent(RoomEvent.ChangeGameMode,this));	//抛出设置游戏模式改变事件
		}
		
		public function getModeCounter():int{
			return this.modeCounter;
		}
		
		public function setModeCounter(modeCounter:int):void{
			this.modeCounter=modeCounter;
			this.dispatchEvent(new RoomEvent(RoomEvent.ChangeModeCounter,this));	//抛出设置模式计数器改变事件
		}
		
		public function getTitle():String{
			return this.roomTitle;
		}
		
		public function setTitle(title:String):void{
			this.roomTitle=title;
		}
		
		public function getRoomType():int{
			return this.roomType;
		}
		
		public function setRoomType(type:int):void{
			this.roomType=type;
		}
		
		public function getComputerCount():int{
			return this.computerCount;
		}
		
		public function setComputerCount(computerCount:int):void{
			this.computerCount=computerCount;
		}
		
		public function getMap():Map{
			return this.map;
		}
		
		public function setMap(mapName:String):void{
			this.map=Map.getMap(mapName);
			this.dispatchEvent(new RoomEvent(RoomEvent.ChangeMap,this));	//抛出设置房主事件
		}
	}
}